﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class DeptController : Controller
    {
        //
        // GET: /Dept/

        public ActionResult Index()
        {
            //List<PhongBan> dsPhongBan = PhongBanModels.GetDSPhongBan(0);
           // ViewBag.danhsachPB = dsPhongBan;
            return View();
        }
        public ActionResult Add()
        {

            return View();
        }
        
        [HttpPost]
        public ActionResult Add(FormCollection fm)
        {
            PhongBan pb = new PhongBan();
            pb.STT = Convert.ToInt32(fm["STT"]);
            pb.TenPhongBan = fm["TenPhongBan"];
            pb.DiaChi      = fm["DiaChi"];
            pb.DienThoai = fm["DienThoai"];
            pb.Fax = fm["Fax"];
            pb.Website = fm["Website"];
            pb.Email = fm["Email"];
            pb.PhanLoai = Convert.ToInt32(fm["PhanLoai"]);
            pb.PhanLoaiHanhChinh = Convert.ToInt32(fm["PhanLoaiHanhChinh"]);
            pb.IDParent = Convert.ToInt32(fm["PhongBan"]);
            PhongBanModels.Add(pb);

            return RedirectToAction("Index");
        }
        public ActionResult Update(int id = 0)
        {
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(id);
            ViewBag.phongban = pb;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection fm, int id = 0)
        {
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(id);
            pb.STT = Convert.ToInt32(fm["STT"]);
            pb.TenPhongBan = fm["TenPhongBan"];
            pb.DiaChi = fm["DiaChi"];
            pb.DienThoai = fm["DienThoai"];
            pb.Fax = fm["Fax"];
            pb.Website = fm["Website"];
            pb.Email = fm["Email"];
            pb.PhanLoai = Convert.ToInt32(fm["PhanLoai"]);
            pb.PhanLoaiHanhChinh = Convert.ToInt32(fm["lstPhanLoaiHanhChinh"]);
            pb.IDParent = Convert.ToInt32(fm["ListPhongBan"]);


            PhongBanModels.Edit(pb);
            return RedirectToAction("Index");
        }
        public ActionResult Delete(int id = 0)
        {
            PhongBanModels.Delete(id);
            return RedirectToAction("Index");
        }
        public int GetConfirmDelete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from cb in context.DSCanBo
                        where cb.IDPhongBan == id
                        select cb;
            return query.ToList().Count;
        }
    }
}
